package com.example.dragon.xmf;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.example.dragon.adpter.AddressAdpter;
import com.example.dragon.bean.AdressBean;
import com.example.dragon.fragment.OrderFragment;
import com.example.dragon.untis.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import static com.example.dragon.untis.ChString.address;


public class MapActivity extends AppCompatActivity implements TextWatcher, PoiSearch.OnPoiSearchListener
,AdapterView.OnItemClickListener{
    private EditText searchText;
    private RecyclerView mrecyclerView;
    List<AdressBean> adressBeanList = new ArrayList<>();
    AddressAdpter addressAdpter;
    LinearLayout minfoLayout;
    String lng="",lat="",Mapaddress;
    EditText addresInfoEdit;
    final int RESULT_CODE=101;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        init();
    }

    /**
     * 初始化AMap对象
     */
    private void init() {
        setUpMap();
    }

    /**
     * 设置页面监听
     */
    private void setUpMap() {
        mrecyclerView = (RecyclerView)findViewById(R.id.addressRecy);
        minfoLayout = (LinearLayout)findViewById(R.id.addressInfo);
        mrecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mrecyclerView.setItemAnimator(new DefaultItemAnimator());
        searchText = (EditText) findViewById(R.id.keyWord);
        addresInfoEdit = (EditText)findViewById(R.id.adinfoEdit);
        searchText.addTextChangedListener(this);// 添加文本输入框监听事件
        seach();
    }
    private void seach(){
        PoiSearch.Query query = new PoiSearch.Query(address,"",MainActivity.CITY);
        query.setPageSize(10);
        query.setPageNum(1);
        PoiSearch poiSearch = new PoiSearch(this,query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        adressBeanList.clear();
        PoiSearch.Query query = new PoiSearch.Query(s.toString(),"",MainActivity.CITY);
        query.setPageSize(10);
        query.setPageNum(1);
        PoiSearch poiSearch = new PoiSearch(this,query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();

    }

    int i =0;
    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        for(i=0;i<poiResult.getPois().size();i++){
            Log.i("GGGG","onPoiSearched== title "+poiResult.getPois().get(i).getTitle()+" getProvinceName "+poiResult.getPois().get(i).getProvinceName()+" getSubPois " +poiResult.getPois().get(i).getSubPois()+" getCityName "+poiResult.getPois().get(i).getCityName()+" getAdName "+poiResult.getPois().get(i).getAdName()+" getDirection "+poiResult.getPois().get(i).getDirection()+" getParkingType "+poiResult.getPois().get(i).getParkingType()+" getSnippet "+poiResult.getPois().get(i).getSnippet()+" getTypeDes "+poiResult.getPois().get(i).getTypeDes()+" getSnippet "+poiResult.getPois().get(i).getSnippet()+" getParkingType "+poiResult.getPois().get(i).getParkingType());
            adressBeanList.add(new AdressBean(
                    poiResult.getPois().get(i).getSnippet(),
                    poiResult.getPois().get(i).getTitle(),
                    poiResult.getPois().get(i).getLatLonPoint().getLatitude()+"",
                    poiResult.getPois().get(i).getLatLonPoint().getLongitude()+"",
                    poiResult.getPois().get(i).getCityName(),
                    poiResult.getPois().get(i).getDirection()));
        }
        addressAdpter = new AddressAdpter(this,adressBeanList);
        mrecyclerView.setAdapter(addressAdpter);
        addressAdpter.notifyDataSetChanged();
        addressAdpter.setOnItemClickLitener(new AddressAdpter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                lat = adressBeanList.get(position).getLat();
                lng = adressBeanList.get(position).getLng();
                Mapaddress=adressBeanList.get(position).getCity()+
                        adressBeanList.get(position).getStreet()+
                        adressBeanList.get(position).getStreetNum()+addresInfoEdit.getText();
                searchText.setText(adressBeanList.get(position).getStreet());

            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
//        Log.i("list",adressBeanList.get(0).toString());
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {
        for(i=0;i<poiItem.getSubPois().size();i++){
            Log.i("GGGG","onPoiSearched=="+poiItem.getSubPois().get(i).getTitle());
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
    public void subAddress(View view){
        Intent intent = new Intent(MapActivity.this,MainActivity.class);
        intent.putExtra("Mapaddress",Mapaddress);
        intent.putExtra("lng",lng);
        intent.putExtra("lat",lat);
        Log.i("Mapaddress",lat+"Mapaddress:"+Mapaddress);
        setResult(0,intent);
        finish();
    }
}
